/**
 * Created by xesam[416249980@qq.com] on 2014/11/6.
 */

var map = new BMap.Map("container");
map.centerAndZoom(new BMap.Point(116.404, 40.3), 9);
map.addControl(new BMap.NavigationControl());
map.addControl(new BMap.ScaleControl());
map.addControl(new BMap.OverviewMapControl());
map.enableScrollWheelZoom();
map.addControl(new BMap.MapTypeControl());


function LayerA() {
    BmapLayer.apply(this, arguments);
}
extend(LayerA, BmapLayer);

function LayerB() {
    BmapLayer.apply(this, arguments);
}
extend(LayerB, BmapLayer);

function AdapterA(data) {
    BmapLayerAdatper.apply(this, arguments);
}
extend(AdapterA, BmapLayerAdatper);
AdapterA.prototype.getItem = function (index) {
    var item = this.data[index];
    var point = new BMap.Point(item['lng'], item['lat']);
    var marker = new BMap.Circle(point, 2000, {
        strokeColor: 'green',
        fillColor: 'green',
        strokeWeight: 2,
        strokeOpacity: 0.6,
        fillOpacity: 0.6
    });
    return marker;
};

function AdapterB(data) {
    BmapLayerAdatper.apply(this, arguments);
}
extend(AdapterB, BmapLayerAdatper);


function mock_ajax(fn) {
    setTimeout(fn, 2000);
}

function $(id) {
    return document.getElementById(id);
}

mock_ajax(function () {
    var layerAProvider = (function () {
        var i = 0;
        var data = [];
        while (i++ < 10) {
            data.push({
                'lng': 116.404 + 0.1 * i,
                'lat': 39.915
            });
        }
        return data;
    })();
    var adapterA = new AdapterA(layerAProvider);
    var layerA = new LayerA();
    layerA.setAdapter(adapterA).addEventListner('click', function (event) {
        console.log('单击layerA:' + event.target.index);
    }).addEventListner('dblclick', function (event) {
        console.log('双击layerA:' + event.target.index);
    });
    layerA.addToMap(map);
    $('demo_btn_clear_a').addEventListener('click', function (event) {
        layerA.removeFromMap(map);
    }, false);
});

mock_ajax(function () {
    var layerBProvider = (function () {
        var i = 0;
        var data = [];
        while (i++ < 10) {
            data.push({
                'lng': 115.404,
                'lat': 39.915 + 0.1 * i
            });
        }
        return data;
    })();
    var adapterB = new AdapterB(layerBProvider);
    var layerB = new LayerB();
    layerB.setAdapter(adapterB).click(function (event) {
        console.log('单击layerB:' + event.target.index);
    });
    layerB.addToMap(map);
    $('demo_btn_clear_b').addEventListener('click', function (event) {
        layerB.removeFromMap(map);
    }, false);
});